package com.mach.platform.api.repository;

import com.mach.platform.api.domain.Attention;
import com.mach.platform.repository.BaseRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface AttentRepo extends BaseRepository<Attention> {

    /**
     * 通过openId和设备组id查询关注信息
     * @param openId 用户openId
     * @param devGroupId 设备组id
     * @return
     */
    @Query(value = "select id from attention where open_id = ?1 and dev_groupId = ?1 and attent_type = 1", nativeQuery = true)
    List<Map> findByOpenIdDevGroupId(String openId, Long devGroupId);

    /**
     * 通过openId和设备ids查询关注信息
     * @param openId 用户openId
     * @param devIds 设备ids
     * @return
     */
    @Query(value = "select dev_id from attention where open_id = ?1 and dev_id in (?2) and attent_type = 2", nativeQuery = true)
    List<Map> findByOpenIdDevIds(String openId, List<Long> devIds);

    /**
     * 通过id修改关注的程度
     * @param id
     */
    @Modifying
    @Query(value = "update attention set attent_level = ?1 where id = ?2", nativeQuery = true)
    int updateLevelById(int level, Long id);

    /**
     * 删除关注的机组
     * @param openId
     * @param type
     * @param groupId
     * @return
     */
    @Modifying
    @Query(value = "delete from attention where open_id = ?1 and attent_type = ?2 and dev_groupId = ?3", nativeQuery = true)
    int deleteByOpenIdTypeGroupId(String openId, int type, Long groupId);

    /**
     * 删除关注的设备
     * @param openId
     * @param type
     * @param devId
     * @return
     */
    @Modifying
    @Query(value = "delete from attention where open_id = ?1 and attent_type = ?2 and dev_id = ?3", nativeQuery = true)
    int deleteByOpenIdTypeDevId(String openId, int type, Long devId);

    /**
     * 查询我关注的设备组
     * @param openId 小程序openId
     * @return
     */
    @Query(value = "select id, dev_groupId, attent_name, attent_level, station_name from attention where attent_type = 1 and open_id = ?1", nativeQuery = true)
    List<Map> findByTypeOpenId(String openId);

    /**
     * 查询我关注的设备组
     * @param openId 小程序openId
     * @return
     */
    @Query(value = "select DISTINCT dev_sortName from attention where attent_type = 2 and open_id = ?1", nativeQuery = true)
    List<Map> findSortNamesByOpenId(String openId);

    /**
     * 通过openId查询我关注的所有设备
     * @param openId
     * @return
     */
    @Query(value = "select id, attent_level, attent_name, dev_id, station_name from attention where open_id = ?1 and attent_type = 2", nativeQuery = true)
    List<Map> findDevByOpenId(String openId);

    /**
     * 通过openId和机组类型查询我关注的设备
     * @param openId
     * @param sortName
     * @return
     */
    @Query(value = "select id, attent_level, attent_name, dev_id, station_name from attention where open_id = ?1 and attent_type = 2 and dev_sortName = ?2", nativeQuery = true)
    List<Map> findDevByOpenIdSortName(String openId, String sortName);

    /**
     * 查询每个设备组的关注人数
     * @param groupIds 设备组ids
     * @return
     */
    @Query(value = "select dev_groupId, count(id) num from attention where dev_groupId in (?1) group by dev_groupId", nativeQuery = true)
    List<Map> getAttNumByGroupIds(List<Long> groupIds);

    /**
     * 查询每个设备的关注人数
     * @param devIds
     * @return
     */
    @Query(value = "select dev_id, count(id) num from attention where dev_id in (?1) group by dev_id", nativeQuery = true)
    List<Map> getAttNumByDevIds(List<Long> devIds);

}
